﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#include "CellStableHeaders.h"
#include "CellLoggerManager.h"
#include "CellException.h"
#include <algorithm>

namespace cell
{

template<> LoggerManager* Singleton<LoggerManager>::ms_Singleton = NULL;

//-----------------------------------------------------------------------
LoggerManager* LoggerManager::getSingletonPtr(void)
{
	return ms_Singleton;
}
LoggerManager& LoggerManager::getSingleton(void)
{  
	assert( ms_Singleton );
	return ( *ms_Singleton );  
}

//-----------------------------------------------------------------------
LoggerManager::LoggerManager()
{
	mDefaultLog = NULL;
}
//-----------------------------------------------------------------------
LoggerManager::~LoggerManager()
{
	CELL_LOCK_AUTO_MUTEX
	// Destroy all logs
	LogList::iterator i;
	for (i = mLogs.begin(); i != mLogs.end(); ++i)
	{
		CELL_DELETE i->second;
	}
}
//-----------------------------------------------------------------------
Logger* LoggerManager::createLogger(const String& name, bool defaultLog,
	bool debuggerOutput, bool suppressTerminalOutput)
{
	CELL_LOCK_AUTO_MUTEX

	Logger* newLog = CELL_NEW Logger(name, debuggerOutput, suppressTerminalOutput);

	if ( !mDefaultLog || defaultLog )
	{
		mDefaultLog = newLog;
	}

	mLogs.insert( LogList::value_type( name, newLog ) );

	return newLog;
}
//-----------------------------------------------------------------------
Logger* LoggerManager::getDefaultLogger()
{
	CELL_LOCK_AUTO_MUTEX
	return mDefaultLog;
}
//-----------------------------------------------------------------------
Logger* LoggerManager::setDefaultLogger(Logger* newLog)
{
	CELL_LOCK_AUTO_MUTEX
	Logger* oldLog = mDefaultLog;
	mDefaultLog = newLog;
	return oldLog;
}
//-----------------------------------------------------------------------
Logger* LoggerManager::getLogger(const String& name)
{
	CELL_LOCK_AUTO_MUTEX
	LogList::iterator i = mLogs.find(name);
	if (i != mLogs.end())
		return i->second;
	else
		CELL_EXCEPT(Exception::ERR_INVALID_PARAMS, "Logger not found. ", "LoggerManager::getLogger");
}
//-----------------------------------------------------------------------
void LoggerManager::destroyLogger(const String& name)
{
	LogList::iterator i = mLogs.find(name);
	if (i != mLogs.end())
	{
		if (mDefaultLog == i->second)
		{
			mDefaultLog = 0;
		}
		CELL_DELETE i->second;
		mLogs.erase(i);
	}

	// Set another default log if this one removed
	if (!mDefaultLog && !mLogs.empty())
	{
		mDefaultLog = mLogs.begin()->second;
	}
}
//-----------------------------------------------------------------------
void LoggerManager::destroyLogger(Logger* logger)
{
	destroyLogger(logger->getName());
}
//-----------------------------------------------------------------------
void LoggerManager::log(const String& message, LogMessageLevel lml, bool maskDebug)
{
	CELL_LOCK_AUTO_MUTEX
	if (mDefaultLog)
	{
		mDefaultLog->log(message, lml, maskDebug);
	}
}
//-----------------------------------------------------------------------
void LoggerManager::setLogDetail(LoggingLevel ll)
{
	CELL_LOCK_AUTO_MUTEX
	if (mDefaultLog)
	{
		mDefaultLog->setLogDetail(ll);
	}
}
//---------------------------------------------------------------------
Logger::Stream LoggerManager::stream(LogMessageLevel lml, bool maskDebug)
{
	CELL_LOCK_AUTO_MUTEX
	if (mDefaultLog)
		return mDefaultLog->stream(lml, maskDebug);
	else
		CELL_EXCEPT(Exception::ERR_INVALID_PARAMS, "Default logger not found. ", "LoggerManager::stream");
}

} // end namespace cell
